package com.huanbao.gateway.exception;


import com.huanbao.common.api.CommonResult;
import com.huanbao.common.api.ResultCode;
import com.huanbao.common.exception.GateWayException;
import io.netty.channel.ConnectTimeoutException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.server.ResponseStatusException;

import java.security.SignatureException;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2020/7/21 16:14
 * @description: TODO  自定义网关异常
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
@Slf4j
@Component
public class GateWayExceptionHandlerAdvice {

    /**
     * 统一 异常
     *
     * @param throwable
     * @return
     */
    @ExceptionHandler(value = {Exception.class})
    public CommonResult handle(Throwable throwable) {

        if (throwable instanceof SignatureException) {
            return signHandle((SignatureException) throwable);
        } else if (throwable instanceof NotFoundException) {
            return notFoundHandle((NotFoundException) throwable);
        } else if (throwable instanceof ResponseStatusException) {
            return handle((ResponseStatusException) throwable);
        } else if (throwable instanceof GateWayException) {
            return badGatewayHandle((GateWayException) throwable);
        } else if (throwable instanceof ConnectTimeoutException) {
            return timeoutHandle((ConnectTimeoutException) throwable);
        } else {
            return CommonResult.failed();
        }

    }

    /**
     * 401 校验 异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {SignatureException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public CommonResult signHandle(SignatureException ex) {
        log.error("SignatureException:{}", ex.getMessage());
        return CommonResult.failed(ResultCode.UNAUTHORIZED);
    }

    /**
     * 404 服务未找到 异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {NotFoundException.class})
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public CommonResult notFoundHandle(NotFoundException ex) {
        log.error("not found exception:{}", ex.getMessage());
        return CommonResult.failed(ResultCode.NOT_FOUND);
    }

    /**
     * 500   其他服务 异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {ResponseStatusException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public CommonResult handle(ResponseStatusException ex) {
        log.error("ResponseStatusException:{}", ex.getMessage());
        return CommonResult.failed(ResultCode.UNAUTHORIZED);
    }

    /**
     * 502 错误网关 异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {GateWayException.class})
    @ResponseStatus(HttpStatus.BAD_GATEWAY)
    public CommonResult badGatewayHandle(GateWayException ex) {
        log.error("badGateway exception:{}", ex.getMessage());
        return CommonResult.failed(ex.getMessage());
    }


    /**
     * 504 网关超时 异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(value = {ConnectTimeoutException.class})
    @ResponseStatus(HttpStatus.GATEWAY_TIMEOUT)
    public CommonResult timeoutHandle(ConnectTimeoutException ex) {
        log.error("connect timeout exception:{}", ex.getMessage());
        return CommonResult.failed(ResultCode.GATEWAY_CONNECT_TIME_OUT);
    }


}
